/*
 * Copyright (C) 2011-2012 Andrew Wells
 *
 * Licensed under the GNU License, Version 3
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.gnu.org/copyleft/gpl.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * Dungeons & Dragons Copyright (C) Wizards of the Coast. I do not claim
 * the right to any information used from their books or resources.
 */

package com.trace.wells.charactertracker;

import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import android.content.Context;
import android.content.res.AssetManager;
import android.util.Log;

public class FileHelper {

	private static final String SPELLFILE_A = "SpellsA.txt";
	private static final String SPELLFILE_C = "SpellsC.txt";
	private static final String SPELLFILE_D = "SpellsD.txt";
	private static final String SPELLFILE_F = "SpellsF.txt";
	private static final String SPELLFILE_H = "SpellsH.txt";
	private static final String SPELLFILE_M = "SpellsM.txt";
	private static final String SPELLFILE_P = "SpellsP.txt";
	private static final String SPELLFILE_S = "SpellsS.txt";
	private static final String SPELLFILE_T = "SpellsT.txt";

	private static final String SPELLFILE_MIN_A = "SpellsAMin.txt";
	private static final String SPELLFILE_MIN_C = "SpellsCMin.txt";
	private static final String SPELLFILE_MIN_D = "SpellsDMin.txt";
	private static final String SPELLFILE_MIN_F = "SpellsFMin.txt";
	private static final String SPELLFILE_MIN_H = "SpellsHMin.txt";
	private static final String SPELLFILE_MIN_M = "SpellsMMin.txt";
	private static final String SPELLFILE_MIN_P = "SpellsPMin.txt";
	private static final String SPELLFILE_MIN_S = "SpellsSMin.txt";
	private static final String SPELLFILE_MIN_T = "SpellsTMin.txt";

	private static final String CHARACTER_LIST = "characterArraylist.txt";
	private static final String CHARACTER_LIST_FILE = "data/data/com.trace.wells.charactertracker/characterArraylist.txt";

	private static final String NOFILE = "NOFILE";

	ArrayList<Character> characterList = new ArrayList<Character>();

	public FileHelper() {
	}

	public FileHelper(ArrayList<Character> charList) {
		characterList = charList;
	}

	public ArrayList<Character> returnCharactersFromFile(String fileName) throws Exception {
		characterList.clear();
		readInCharacter(fileName);

		return characterList;
	}

	public ArrayList<String> getStringCharacterList() {
		File characterListFile = new File(CHARACTER_LIST_FILE);
		if (!characterListFile.exists()) {
			try {
				characterListFile.createNewFile();
				this.writeStringCharacters(CHARACTER_LIST_FILE, new ArrayList<String>());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		ArrayList<String> characterArrayList = null;
		ObjectInputStream inputStream = null;
		try {

			//Construct the ObjectInputStream object
			inputStream = new ObjectInputStream(new FileInputStream(CHARACTER_LIST_FILE));

			characterArrayList = (ArrayList<String>) inputStream.readObject();

		} catch (EOFException ex) { //This exception will be caught when EOF is reached
			Log.d("CHAR", "Reached EOF");
		} catch (ClassNotFoundException ex) {
			Log.d("CHAR", "ClassNotFoundException");
		} catch (FileNotFoundException ex) {
			Log.d("CHAR", "FileNotFoundException");
		} catch (IOException ex) {
			Log.d("CHAR", "IOException");
		} finally {
			//Close the ObjectInputStream
			try {
				if (inputStream != null) {
					inputStream.close();
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		return characterArrayList;
	}

	public Character getCharacterObject(String fileName) {
		Character returnCharacter = null;

		return returnCharacter;
	}

	public void writeStringCharacters(String characterListFile, ArrayList<String> arrayList) {
		ObjectOutputStream outputStream = null;

		try {
			//Construct the LineNumberReader object
			outputStream = new ObjectOutputStream(new FileOutputStream(characterListFile));

			outputStream.writeObject(arrayList);

		} catch (FileNotFoundException ex) {
			Log.d("CHAR", "FileNotFoundException");
		} catch (IOException ex) {
			Log.d("CHAR", "IOException");
		} finally {
			//Close the ObjectOutputStream
			try {
				if (outputStream != null) {
					outputStream.flush();
					outputStream.close();
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}

	}

	public Character readInCharacter(String filename) throws Exception {

		Character returnCharacter = null;
		//ArrayList<Character> characterList = new ArrayList<Character>();
		ObjectInputStream inputStream = null;

		try {

			//Construct the ObjectInputStream object
			inputStream = new ObjectInputStream(new FileInputStream(ConstantHelper.FILE_DIRECTORY + filename));

			returnCharacter = (Character) inputStream.readObject();

		} catch (EOFException ex) { //This exception will be caught when EOF is reached
			Log.d("CHAR", "Reached EOF");
		} catch (ClassNotFoundException ex) {
			Log.d("CHAR", "ClassNotFoundException");
		} catch (FileNotFoundException ex) {
			Log.d("CHAR", "FileNotFoundException");
		} catch (IOException ex) {
			Log.d("CHAR", "IOException");
		} finally {
			//Close the ObjectInputStream
			try {
				if (inputStream != null) {
					inputStream.close();
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}

		return returnCharacter;
	}

	public static void writeSingleCharacter(String filePath, Character character) {
		ObjectOutputStream outputStream = null;

		try {

			File file = new File(filePath);
			file.delete();
			file.createNewFile();

			outputStream = new ObjectOutputStream(new FileOutputStream(filePath));

			outputStream.writeObject(character);

		} catch (FileNotFoundException ex) {
			Log.d("CHAR", "FileNotFoundException");
		} catch (IOException ex) {
			Log.d("CHAR", "IOException");
		} finally {
			try {
				if (outputStream != null) {
					outputStream.flush();
					outputStream.close();
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
	}

	public void writeCharacters(String filename, ArrayList<Character> charArray) {

		ObjectOutputStream outputStream = null;

		try {

			//Construct the LineNumberReader object
			outputStream = new ObjectOutputStream(new FileOutputStream(filename));

			for (Character ch : charArray) {
				outputStream.writeObject(ch);
				Log.d("CHAR", "WROTE OUT: " + ch.info.getName());
			}

		} catch (FileNotFoundException ex) {
			Log.d("CHAR", "FileNotFoundException");
		} catch (IOException ex) {
			Log.d("CHAR", "IOException");
		} finally {
			//Close the ObjectOutputStream
			try {
				if (outputStream != null) {
					outputStream.flush();
					outputStream.close();
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
	}

	public void removeCharacterFile(String fileName) {
		File f = new File(ConstantHelper.FILE_DIRECTORY + fileName);
		f.delete();
	}

	public Spell getSpellInformationBasedOnName(String spellName, Context context, int charClass) {
		Spell returnSpell = null;
		try {
			InputStream is = null;
			String fileName = getFileNameFromSpellName(spellName);
			AssetManager assets = context.getAssets();
			is = assets.open(fileName);
			returnSpell = ConstantSpellHelper.getSpellData(spellName, is, charClass);

		} catch (EOFException ex) { //This exception will be caught when EOF is reached
			Log.d("CHAR", "Reached EOF");
		} catch (FileNotFoundException ex) {
			Log.e("CHAR", "FileNotFoundException");
		} catch (IOException ex) {
			Log.e("CHAR", "IOException");
		} finally {

		}
		return returnSpell;

	}

	public SpellListItem getMinSpellInformationBasedOnName(String spellName, PrepareSpells prepareSpells, int charClass) {
		SpellListItem returnSpell = null;
		try {
			InputStream is = null;
			String fileName = getFileNameFromSpellName(spellName);
			AssetManager assets = prepareSpells.getAssets();
			is = assets.open(fileName);
			returnSpell = ConstantSpellHelper.getMinimumSpellData(spellName, is, charClass);

		} catch (EOFException ex) { //This exception will be caught when EOF is reached
			Log.d("CHAR", "Reached EOF");
		} catch (FileNotFoundException ex) {
			Log.e("CHAR", "FileNotFoundException");
		} catch (IOException ex) {
			Log.e("CHAR", "IOException");
		} finally {

		}
		return returnSpell;

	}

	public static String getFileNameFromSpellName(String spellName) {
		String retStr;
		String beginningCharacter = spellName.substring(0, 1).toLowerCase();
		if (beginningCharacter.compareTo("a") >= 0 && beginningCharacter.compareTo("b") <= 0) {
			retStr = SPELLFILE_A;
		} else if (beginningCharacter.compareTo("c") >= 0 && beginningCharacter.compareTo("c") <= 0) {
			retStr = SPELLFILE_C;
		} else if (beginningCharacter.compareTo("d") >= 0 && beginningCharacter.compareTo("e") <= 0) {
			retStr = SPELLFILE_D;
		} else if (beginningCharacter.compareTo("f") >= 0 && beginningCharacter.compareTo("g") <= 0) {
			retStr = SPELLFILE_F;
		} else if (beginningCharacter.compareTo("h") >= 0 && beginningCharacter.compareTo("l") <= 0) {
			retStr = SPELLFILE_H;
		} else if (beginningCharacter.compareTo("m") >= 0 && beginningCharacter.compareTo("o") <= 0) {
			retStr = SPELLFILE_M;
		} else if (beginningCharacter.compareTo("p") >= 0 && beginningCharacter.compareTo("r") <= 0) {
			retStr = SPELLFILE_P;
		} else if (beginningCharacter.compareTo("s") >= 0 && beginningCharacter.compareTo("s") <= 0) {
			retStr = SPELLFILE_S;
		} else if (beginningCharacter.compareTo("t") >= 0 && beginningCharacter.compareTo("z") <= 0) {
			retStr = SPELLFILE_T;
		} else {
			retStr = NOFILE;
		}
		return retStr;
	}

	public static String getMinimumFileNameFromSpellName(String spellName) {
		String retStr;
		String beginningCharacter = spellName.substring(0, 1).toLowerCase();
		if (beginningCharacter.compareTo("a") >= 0 && beginningCharacter.compareTo("b") <= 0) {
			retStr = SPELLFILE_MIN_A;
		} else if (beginningCharacter.compareTo("c") >= 0 && beginningCharacter.compareTo("c") <= 0) {
			retStr = SPELLFILE_MIN_C;
		} else if (beginningCharacter.compareTo("d") >= 0 && beginningCharacter.compareTo("e") <= 0) {
			retStr = SPELLFILE_MIN_D;
		} else if (beginningCharacter.compareTo("f") >= 0 && beginningCharacter.compareTo("g") <= 0) {
			retStr = SPELLFILE_MIN_F;
		} else if (beginningCharacter.compareTo("h") >= 0 && beginningCharacter.compareTo("l") <= 0) {
			retStr = SPELLFILE_MIN_H;
		} else if (beginningCharacter.compareTo("m") >= 0 && beginningCharacter.compareTo("o") <= 0) {
			retStr = SPELLFILE_MIN_M;
		} else if (beginningCharacter.compareTo("p") >= 0 && beginningCharacter.compareTo("r") <= 0) {
			retStr = SPELLFILE_MIN_P;
		} else if (beginningCharacter.compareTo("s") >= 0 && beginningCharacter.compareTo("s") <= 0) {
			retStr = SPELLFILE_MIN_S;
		} else if (beginningCharacter.compareTo("t") >= 0 && beginningCharacter.compareTo("z") <= 0) {
			retStr = SPELLFILE_MIN_T;
		} else {
			retStr = NOFILE;
		}
		return retStr;
	}

	public String convertCharacterInfoStringToFileName(String characterString) {

		String delimiter = "\\:";
		String[] split = characterString.split(delimiter);

		String retStr = split[0].trim() + split[4].trim();
		return retStr;
	}

}
